from mongo import jokes_col


def import_jokes_from_file(file_path):
    # 读取文件内容
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 按空行分割笑话（处理可能的多空行情况）
    jokes = [joke.strip() for joke in content.split('\n\n') if joke.strip()]
    
    # 准备插入的数据格式
    jokes_data = [{"content": joke} for joke in jokes]
    
    if jokes_data:
        # 插入到数据库
        result = jokes_col.insert_many(jokes_data)
        print(f"成功插入 {len(result.inserted_ids)} 条笑话")
    else:
        print("未找到有效的笑话内容")

# 调用函数导入笑话（确保joker.txt与脚本在同一目录或填写正确路径）
import_jokes_from_file("D:\casket\python_git\joke_ai\db\joker.txt")